package com.pethub.dao.daoimpl;

import com.pethub.dao.ProviderDao;
import com.pethub.entity.Provider;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("providerDao")
public class ProviderDaoImpl implements ProviderDao{
    @Autowired
    private SessionFactory sessionFactory;

    private Session getCurrentSession() {
        return this.sessionFactory.getCurrentSession();
    }
    public Provider load(Integer id) throws Exception {
        return (Provider) this.getCurrentSession().load(Provider.class,id);
    }

    public Provider get(Integer id) throws Exception {
        return (Provider) this.getCurrentSession().get(Provider.class,id);
    }

    public List<Provider> findAll(String hql) throws Exception {
        return this.getCurrentSession().createQuery(hql).setCacheable(true).list();
    }

    public Provider save(Provider entity) throws Exception {
        Integer id = (Integer) getCurrentSession().save(entity);
        flush();
        return this.load(id);
    }

    public void saveOrUpdate(Provider entity) throws Exception {
        this.getCurrentSession().saveOrUpdate(entity);
    }

    public void delete(Integer id) throws Exception {
        this.getCurrentSession().delete(this.get(id));
    }

    public void flush() throws Exception {
        this.getCurrentSession().flush();
    }
}
